/**
 ****************************************************************************
 * Copyright (C) Marcelo F. Ochoa. All rights reserved.                      *
 * ------------------------------------------------------------------------- *
 * This software is published under the terms of the Apache Software License *
 * version 1.1, a copy of which has been included  with this distribution in *
 * the LICENSE file.                                                         *
 */

package org.apache.cocoon.caching;

import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.components.cache.ESIKey;
import org.apache.cocoon.components.cache.Server;

/**
 * A validation object for External invalidation functionality.
 * This is might be the most used CacheValidity object for DB Prism.
 * @author <a href="mailto:mochoa@ieee.org">Marcelo F. Ochoa</a>
 */
public final class ExternalCacheValidity implements CacheValidity {
    private ESIKey key;
    private static Server cacheServer;

    public ExternalCacheValidity(ESIKey key) {
        this.key  = key;
    }

    public boolean isValid(CacheValidity validity) {
        boolean exists = false;
        if (validity instanceof ExternalCacheValidity) {
            try {
              exists = this.cacheServer.isRegistered(this.key);
              return exists;
            } catch (ProcessingException pe) {}
        }
        return exists;
    }

    public long getKey() {
        return this.key.getKey();
    }

    public String toString() {
        return "ExternalCacheValidity: " + this.key;
    }

    public static void registerCacheServer(Server globalCacheServer) {
        cacheServer = globalCacheServer;
    }
}
